//
//  ViewController3.m
//  someDemo
//
//  Created by Reinhard-X on 2016/12/1.
//  Copyright © 2016年 上官惠阳. All rights reserved.
//

#import "ViewController3.h"
#import "UITextField+Add.h"
#import "UIButton+timer.h"

@interface ViewController3 ()<UITextFieldDelegate>

@end

@implementation ViewController3
{
    NSString *_verCode;
    NSString *_phoneNum;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.regBtn.layer.cornerRadius = 5;
    self.regBtn.layer.masksToBounds = YES;
    
    _pwdField.delegate = self;
    [self setupForDismissKeyboard];
}
//?m=appface&c=sms&a=send_reg_code   获取注册验证码接口 参数：mobile:手机号 token:Jg6uH5ZAkksqaFco

//?m=appface&c=user&a=reg  注册接口 参数：mobile:手机号 pwd:密码
- (IBAction)sendCodeAction:(id)sender
{
    if (!_phoneNumField.text.length) {
        [_phoneNumField addShakeAnimation];
        [HUDTool showInView:self.view withText:@"请输入手机号"];
        return;
    }
    if (![QZChectTool isPhoneNumber:_phoneNumField.text]){
        [HUDTool showInView:self.view withText:@"请输入者正确格式的手机号"];
        return;
    }
    __weak typeof(self) weakSelf = self;
    NSDictionary *dic = @{@"mobile":_phoneNumField.text,
                          @"token":@"Jg6uH5ZAkksqaFco"};
    [MHNetworkManager postReqeustWithURL:URL(@"?m=appface&c=sms&a=send_reg_code") params:dic successBlock:^(NSDictionary *returnData) {
        NSLog(@"returnData = %@",returnData);
        if (ResultJudge){
            _verCode = returnData[@"data"][@"code"];
            _phoneNum = _phoneNumField.text;
            [weakSelf.sendCodeBtn addTimer];
        }else{
            ShowResultMsg;
        }
    } failureBlock:^(NSError *error) {
        DTLog(@"error = %@",error);
        ShowFailMsg;
    } showHUD:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)regBtnAction:(id)sender {
    if (!_phoneNumField.text.length) {
        [_phoneNumField addShakeAnimation];
        ShowSomeMsg(@"请输入手机号");
        return;
    }
    if (![_phoneNumField.text isEqualToString:_phoneNum]) {
        ShowSomeMsg(@"请请输入获取验证码的手机号");
        return;
    }
    if (!_VerCodeFeild.text.length) {
        [_VerCodeFeild addShakeAnimation];
        ShowSomeMsg(@"请输入验证码");
        return;
    }
    if (![_VerCodeFeild.text isEqualToString:_verCode]) {
        ShowSomeMsg(@"验证码不正确");
        return;
    }
    if (!_pwdField.text.length) {
        [_pwdField addShakeAnimation];
        ShowSomeMsg(@"请输入密码");
        return;
    }
    __weak typeof(self) weakSelf = self;
    NSDictionary *dic = @{@"mobile":_phoneNumField.text,
                          @"pwd":_pwdField.text};
    [MHNetworkManager postReqeustWithURL:URL(@"?m=appface&c=user&a=reg") params:dic successBlock:^(NSDictionary *returnData) {
        NSLog(@"returnData = %@",returnData);
        if (ResultJudge) {
            ShowSomeMsg(@"注册成功");
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else{
            ShowResultMsg;
        }
    } failureBlock:^(NSError *error) {
        DTLog(@"error = %@",error);
        ShowFailMsg;
    } showHUD:YES];
}
- (IBAction)protBtnAction:(id)sender {
    
}

- (IBAction)exitBtnActon:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
@end
